package org.cloud.listener;

import com.netflix.appinfo.InstanceInfo;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRenewedEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaServerStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class EurekaStateChangeListener {

    @EventListener
    // 处理服务下线事件
    public void Listener(EurekaInstanceCanceledEvent event) {
        // 服务断线事件
        String appName = event.getAppName();
        String serverId = event.getServerId();
        System.out.printf(" 服务: %S(%S) 下线了 \n", appName, serverId);
    }

    @EventListener
    // 处理注册事件
    public void Listener(EurekaInstanceRegisteredEvent event) {
        // 服务断线事件
        InstanceInfo info = event.getInstanceInfo();
        String appName = info.getAppName();
        System.out.printf(" 服务: %S 正在注册 \n", appName);
    }


    @EventListener
    // 服务续约事件
    public void Listener(EurekaInstanceRenewedEvent event) {
        String appName = event.getAppName();
        String serverId = event.getServerId();
        System.out.printf(" 续约名为: %S(%S) 的服务 \n", appName, serverId);
    }

    @EventListener
    // 注册中心启动事件
    public void Listener(EurekaServerStartedEvent event) {
        System.out.println("EurekaServer 启动了 ");
    }

}
